﻿<template>
	<div class="busAttribute-container">
		<el-dialog v-model="isShowDialog" :title="props.title" :width="700" draggable="">
			<el-form :model="ruleForm" ref="ruleFormRef" size="default" label-width="100px" :rules="rules">
				<el-row :gutter="35">
					<el-form-item v-show="false">
						<el-input v-model="ruleForm.id" />
					</el-form-item>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="属性名称" prop="aName">
							<el-input v-model="ruleForm.aName" :disabled="ruleForm.id == null ? false : true" placeholder="请输入属性名称" clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="值编号" prop="paId">
							<el-input v-model="ruleForm.paId" placeholder="请输入值编号" clearable  :disabled="true" />
						</el-form-item>
					</el-col>
					<!-- <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="值" prop="aValue">
							<el-input v-model="ruleForm.aValue" placeholder="请输入值" clearable />
						</el-form-item>
					</el-col> -->
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-button icon="ele-Plus" type="primary" plain @click="handleAdd()"> 添加</el-button>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<template v-if="GetAttriValuesListInput && GetAttriValuesListInput.length > 0">
							<el-row :gutter="35" v-for="(item, index) in GetAttriValuesListInput" :key="index">
								<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
									<el-form-item label="值" prop="aValue">
										<el-input v-model="item.value" placeholder="请输入值" clearable />
									</el-form-item>
								</el-col>
								<el-col :span="12">
									<el-button size="mini" type="text" icon="el-icon-circle-plus-outline" @click="handleAdd()">添加</el-button>
									<el-button size="mini" type="text" icon="el-icon-remove-outline" @click="handleDel(index)">删除</el-button>
								</el-col>
							</el-row>
						</template>
					</el-col>
					
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="排序" prop="aOrder">
							<el-input-number v-model="ruleForm.aOrder" placeholder="请输入排序" clearable />
						</el-form-item>
						
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="cancel" size="default">取 消</el-button>
					<el-button type="primary" @click="submit" size="default">确 定</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script lang="ts" setup>
	import { ref,onMounted } from "vue";
	import { ElMessage } from "element-plus";
	import type { FormRules } from "element-plus";
	import { addBusAttribute, updateBusAttribute,GetAttriValuesList } from "/@/api/main/busAttribute";
	import { getDictDataList } from '/@/api/system/admin';


	const getEditisDeleteData = ref<any>([]);

	var props = defineProps({
	title: {
	type: String,
	default: "",
	},
	});
	//父级传递来的函数，用于回调
	const emit = defineEmits(["reloadTable"]);
	const ruleFormRef = ref();
	const isShowDialog = ref(false);
	const ruleForm = ref<any>({});
		//自行添加其他规则
		const rules = ref<FormRules>({});


// 打开弹窗
const openDialog = (row: any, isUpdate: any) => {
	if (isUpdate == "1") {
		ruleForm.value = JSON.parse(JSON.stringify(row));
		// console.log(JSON.parse(JSON.stringify(row)));
	}
//   GetAttriValuesListInput.value.length = 0;
  isShowDialog.value = true;
  if(row.id!= undefined && ruleForm.value.id > 0)
  {
	getAttriValuesList(row.id);
  }
  
};

// 关闭弹窗
const closeDialog = () => {
	GetAttriValuesListInput.value.length = 0;
  emit("reloadTable");
  isShowDialog.value = false;
};

// 取消
const cancel = () => {
	GetAttriValuesListInput.value.length = 0;
  isShowDialog.value = false;
};
const avalue=ref<any>([]);
// 提交
const submit = async () => {
  ruleFormRef.value.validate(async (isValid: boolean, fields?: any) => {
    if (isValid) {
      let values = ruleForm.value;

	  GetAttriValuesListInput.value.forEach((item: any) => avalue.value.push(item.value));//
	  values.avalues=avalue.value;
      if (ruleForm.value.id != undefined && ruleForm.value.id > 0) {
        await updateBusAttribute(values);
      } else {
        await addBusAttribute(values);
      }
      closeDialog();
    } else {
      ElMessage({
        message: `表单有${Object.keys(fields).length}处验证失败，请修改后再提交`,
        type: "error",
      });
    }
  });
};

    const dictTypeDataList = async (val: any) => {
      let list = await getDictDataList(val);
      return list.data.result ?? [];
    };

	const GetAttriValuesListInput = ref<any>([]);
const getAttriValuesList = async (value: any) => {
    let list = await GetAttriValuesList({ p1: value });
    GetAttriValuesListInput.value = list.data.result ?? [];
}
// getAttriValuesList(null);
//添加一行方法
// const addInput =async () => {
// 	this.getAttriValuesList.value.push(1)  //通过添加array的值，增加input的个数
//     };
//     //删除当前行方法
//     delInput(async (item,index) => {
//       this.personalInfo.expertiseListVo.splice(index,1)
//     });
const list = ref([]);

const handleAdd = () => {
	GetAttriValuesListInput.value.push({})
}

const handleDel = (index: number) => {
	GetAttriValuesListInput.value.splice(index, 1)
}


// 页面加载时
onMounted(async () => {
	     getEditisDeleteData.value= await dictTypeDataList('data_yes_no');
});

//将属性或者函数暴露给父组件
defineExpose({ openDialog });
</script>




